var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
    type:'juli',
		longitude:'',
		latitude:'',
		datalist: [],
		pagenum: 1,
		keyword:'',
        cid:0,
        diqulist:[],
        showbox:false,
        region:[],   //定义一个空数组用来存放选择地具体是哪一个省市区
        province:'',
        city:'',
        district:'',
	},
	_onLoad:function(){
		var that = this
		if(that.options && that.options.cid){
			that.setData({cid:that.options.cid});
		}
        $.get('yuxun/getlist', {  }, function (res) {
            that.setData(res);
        });
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
		wx.getLocation({
			type: 'gcj02',
			success:function(res) {
				var latitude = res.latitude
				var longitude = res.longitude
				that.setData({longitude:longitude,latitude:latitude})
				that.getDataList();
			},
			fail:function(){
				that.getDataList();
			}
		})
  },
  getUserProvince:function(e)
  {
     this.setData({
         region:e.detail.value,    //将用户选择的省市区赋值给region
         province:e.detail.value[0],
         city:e.detail.value[1],
         district:e.detail.value[2],
     })
     this.getDataList()
     console.log('地区',this.data.region,this.data.province,this.data.city,this.data.district,e)
  },

	getDataList: function () {
		var that = this
		var pagenum = that.data.pagenum
		var latitude = that.data.latitude
		var longitude = that.data.longitude
		var keyword = that.data.keyword
		console.log(keyword)
		that.setData({loaddingmore:true})
        $.post('yuxun/getlist', {province:that.data.province,city:that.data.city,district:that.data.district, pagenum: pagenum, cid: that.data.cid, field:that.data.type,order:'desc',longitude:longitude,latitude:latitude,keyword:keyword}, function (res) {
			that.setData({loaddingmore:false})
			var data = res.data
			if (pagenum == 1) {
        that.setData({ datalist: data });
        console.log('list11',data)
				if (data.length == 0) {
					$.showEmpty('暂无相关数据！');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
          that.setData({ datalist: newdata });
          console.log('list2',newdata)
				}
			}
			that.setData({
				sclist: data
			});
		},false);
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
    },
    showbox:function(){
        this.setData({showbox:true})
    },
    hidebox:function(){
        this.setData({showbox:false})
    },
  changetab:function(e){
    var that = this;
    var cid = e.currentTarget.dataset.cid
    that.setData({ cid: cid })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
    //this.onLoad()
		that.getDataList();
  },
	search:function(e){
    var that = this;
		var keyword = e.detail.value;
    that.setData({ keyword: keyword })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
		that.getDataList();

	},
  sortClick:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type
    that.setData({ type: type })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
    that.getDataList();
  },
  filterClick:function(e){
    var that = this;
    var types = e.currentTarget.dataset.types
    that.setData({ types: types })

  }
}))